import typing as T
from fastapi import Depends
from openai import AsyncOpenAI, OpenAI
import logging
from .Global import ENV
from ..common.pool import OpenAiPool, PoolConfig


_pool = OpenAiPool(base_url=ENV.BASE_URL, api_key=ENV.API_KEY, config=PoolConfig())


def get_temp_client() -> OpenAI:
    return OpenAI(api_key=ENV.API_KEY, base_url=ENV.BASE_URL)


def get_temp_async_client() -> AsyncOpenAI:
    return AsyncOpenAI(api_key=ENV.API_KEY, base_url=ENV.BASE_URL)


async def get_openai_client():
    async with _pool.acquire() as client:
        yield client


OpenAISession = T.Annotated[AsyncOpenAI, Depends(get_openai_client)]  # openai异步客户端

logging.info("openai客户端初始化完成...")
